package com.hnucm.chapter7_1;



import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class DemoActivity_1 extends Activity {
    private static final String TAG = "DemoActivity_1";


    private TextView mButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo1);
        initView();

        mButton=findViewById(R.id.button1);
    }

    private void initView() {

    }

    //2.用一个类包装原始对象，间接为其提供get和set方法。

    public void onClick(View v){
        if(v==mButton){
            ObjectAnimator.ofInt(mButton,"width",500).setDuration(5000).start();
//            performAnimation();
        }
    }

    private void performAnimation() {
        ViewWrapper viewWrapper=new ViewWrapper(mButton);
        android.animation.ObjectAnimator.ofInt(viewWrapper,"width",mButton.getWidth(),500)
                .setDuration(5000).start();
    }

    private static class ViewWrapper{
        private View mTarget;
        public ViewWrapper(View target){
            mTarget=target;
        }
        public int getWidth(){
            return mTarget.getLayoutParams().width;
        }
        public void setWidth(int width){
            mTarget.getLayoutParams().width=width;
            mTarget.requestLayout();
        }
    }


//    3.采用ValueAnimator，监听动画过程，自己实现属性的改变
//    @Override
//    public void onWindowFocusChanged(boolean hasFocus) {
//        super.onWindowFocusChanged(hasFocus);
//        if (hasFocus) {
//            Button button = (Button)findViewById(R.id.button1);
//            performAnimate(button, button.getWidth(), 500);
//        }
//    }
//
//    Animation.AnimationListener listener;
//    Animator.AnimatorListener listener1;
//    ValueAnimator.AnimatorUpdateListener animatorUpdateListener;
//
//    private void performAnimate(final View target, final int start, final int end) {
//        ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100);
//        valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
//
//            // 持有一个IntEvaluator对象，方便下面估值的时候使用
//            private IntEvaluator mEvaluator = new IntEvaluator();
//
//            @Override
//            public void onAnimationUpdate(ValueAnimator animator) {
//                // 获得当前动画的进度值，整型，1-100之间
//                int currentValue = (Integer) animator.getAnimatedValue();
//                Log.d(TAG, "current value: " + currentValue);
//
//                // 获得当前进度占整个动画过程的比例，浮点型，0-1之间
//                float fraction = animator.getAnimatedFraction();
//                // 直接调用整型估值器通过比例计算出宽度，然后再设给Button
//                target.getLayoutParams().width = mEvaluator.evaluate(fraction, start, end);
//                target.requestLayout();
//            }
//        });
//
//        valueAnimator.setDuration(5000).start();
//    }

}
